package com.itheima.service.impl;

import com.itheima.dao.ReportDao;
import com.itheima.service.ReportService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service(interfaceClass = ReportService.class)
//@Service
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;

    /**
     * 会员每日新增（总量）变化数据查询与封装
     * @return 每日新增（总量）变化数据集
     */
    @Override
    public Map<String,List> getMemberNumChange() {

        //封装 一年的月份
        //String[] months = new String[12];
        //Integer[] countMember = new Integer[12];
        List<String> months = new ArrayList<>();
        List<Integer> countMember = new ArrayList<>();

        //初始化当前日期
        Calendar rightNow = Calendar.getInstance();
        //获取当前年
        int year = rightNow.get(Calendar.YEAR);
        //获取月
        int month = rightNow.get(Calendar.MONTH);
        /*//获取日
        int date = rightNow.get(Calendar.DATE);*/
        //System.out.println(month);
        int temp = month+1;
        int j = -1;
        for (int i = 0; i < 12; i++) {
            j++;
            month = temp - j;

            String startTime = null;
            String endTime = null;
            if (month == 0) { //当 month 被减至0时将其重置为 12
                temp = month = 12;
                year = year - 1;
                j = 0;
                startTime = year + "-" + month + "-" + "01";
                endTime = year + "-" + month + "-" + "31";
            }else{
                startTime = year + "-" + month + "-" + "01";
                endTime = year + "-" + month + "-" + "31";
            }

            int number = reportDao.getMemberCountByYear(startTime, endTime);
            //sumMember += number;
            //countMember.add(sumMumber); //显示本月及以前的总会员数
            months.add(year+"."+month);
            countMember.add(number); // 本月新增会员数
        }
        //反转集合 将 集合由 2021.9 2021.8 2021.7... --> ...2021.7 2021.8 2021.9
        Collections.reverse(months);
        Collections.reverse(countMember);
        //创建Map集合对象 --> 存入所需的数据返回controller
        Map<String,List> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",countMember);
        return map;
    }

    /**
     * 套餐预约数据查询与封装
     * @return 套餐预约数据集
     */
    @Override
    public Map<String,List> getSetmealReport() {
        List<Map<String, String>> setmealReport = reportDao.getSetmealReport();

        List<String> setmealName = new ArrayList<>();
        for (Map<String, String> map : setmealReport) {
            String name = map.get("name");
            setmealName.add(name);
        }
        Map<String,List> map = new HashMap<>();
        map.put("setmealNames",setmealName);
        map.put("setmealCount",setmealReport);
        return map;
    }

    public Map getBusinessReportData(){



        return null;
    }
}

